import sys; sys.path.insert(0,'third_party'); sys.path.insert(0,'.')
from twisted.internet import reactor,threads
from common import crypto, RPCServer

class Echoer():
    rpc_port = 8080
    crypto_db = 'pb_server.sqlite'    
    def echo(self, st):
        print 'echoing:', st
        return st
    def get_public_key(self):
        crypto.generate_keys(self.crypto_db)
        print "get_public_key"        
        pubkey = crypto.get_public_key(self.crypto_db)
        print "pubkey = ", pubkey
        return pubkey
    def largeFibonacciNumber(self):
        return threads.deferToThread(self._largeFibonacciNumber)
    def _largeFibonacciNumber(self):
        """
        Represent a long running blocking function by calculating
        the TARGETth Fibonnaci number
        """
        TARGET = 100
    
        first = 0
        second = 1
    
        for i in xrange(TARGET - 1):
            new = first + second
            first = second
            second = new
    
        return second


if __name__ == '__main__':
    rs = RPCServer(Echoer())
    reactor.run()